<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class Delete extends CApiComponent
{
        private $_action = 1;
    private $_userNick;
    
    public function __construct ()
    {
        parent::init ();
        $this->result = array();
        $this->result["state"] = false;
        $this->result["code"]  = 0;
        $this->result["msg"] = "删除失败";
        $this->result["msg_code"] = "";
        $this->result["data"] = array("d" => false);
    }
    
    public function handleResult($status, $code, $message) {
        $this->result["state"] = true;
        $this->result["code"]  = 0;
        $this->result["msg"] = $message;
        $this->result["msg_code"] = "";
        $this->result["data"] = array("d" => $status);     }
    
    public function invoke($ids) {
        $device = new UserDevice();
        $device = $device->findByUserIdAndType($this->_userId, 1);
        $this->_deviceId = $device["id"];
        
        $user  = User::model()->findByPk($this->_userId);
        $this->_userNick = $user["user_name"];
        
        $hander = new UserFile();
        $files = $hander->getFilesByIds($this->_userId, $ids);
                foreach ($files as $file) {
            if ($file["is_deleted"]) continue;               if ($file["file_type"] == 0) {
                $file["is_deleted"] = 1;
                $file["event_uuid"] = CUtils::random_string(46);
                $file->save();
                                $this->handleFileMeta($file["file_path"], $file["version_id"], $this->_userId, $this->_userNick);
                                $event = new UserEvent();
                $event->CreateEvent(
                                    $this->_userId, 
                                    $this->_deviceId, 
                                    $this->_action, 
                                    $file["file_path"], 
                                    $file["file_path"], 
                                    $file["event_uuid"]
                                    );
            } else {
                $this->delete($file);
            }
        }
        $this->handleResult(true, 0, "删除成功");
    }
    
    
    public function delete($parenFile) {
                $parentPath = $parenFile["file_path"];
        $handler = new UserFile();
        $files = $handler->getFilesByPath($parentPath);
                foreach ($files as $file) {
            if ($file["is_deleted"]) continue;               $file["event_uuid"] = CUtils::random_string(46);
            if ($file["file_type"] == 0) {
                if ($file["is_deleted"]) continue;                   $file["is_deleted"] = 1;
                $file->save();
                                $this->handleFileMeta($file["file_path"], $file["version_id"], $this->_userId, $this->_userNick);
                                $event = new UserEvent();
                $event->CreateEvent(
                                    $this->_userId, 
                                    $this->_deviceId, 
                                    $this->_action, 
                                    $file["file_path"], 
                                    $file["file_path"], 
                                    $file["event_uuid"]
                                    );
                continue;
            }
            $this->delete($file);
            $file["is_deleted"] = 1;
            $file->save();
                        $event = new UserEvent();
            $event->CreateEvent(
                                $this->_userId,
                                $this->_deviceId, 
                                $this->_action, 
                                $file["file_path"], 
                                $file["file_path"], 
                                $file["event_uuid"]
                                );
        }
        $parenFile["event_uuid"] = CUtils::random_string(46);
        $parenFile["is_deleted"] = 1;
        $parenFile->save();
                $event = new UserEvent();
        $event->CreateEvent(
                            $this->_userId,
                            $this->_deviceId, 
                            $this->_action, 
                            $parenFile["file_path"], 
                            $parenFile["file_path"], 
                            $parenFile["event_uuid"]
                            );
    }
    
    
    public function handleFileMeta($filePath, $versionId, $userId, $userNick) {
                                $meta = new FileMeta();
        $meta = $meta->getFileMeta($filePath, "version");
        if ($meta) {
            $value = CUtils::getFileVersions($versionId, $this->_action, $userId, $userNick, $meta["meta_value"]);
            $meta["meta_value"] = $value;
        } else {
            $value = CUtils::getFileVersions($versionId, $this->_action, $userId, $userNick);
            $meta["file_path"]  = $filePath;
            $meta["meta_key"]   = "version";
            $meta["meta_value"] = $value;
        }
        return $meta->save();
    }
    
    public function handleException($exception) {
        echo CJSON::encode ( $this->result );
    }
}
?>